import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class upDate {
    public static void updateRecord(int idToUpdate, String newItemName, String newValue,String url,String user,String password) {
        try {
            // SQL语句：根据ID更新记录
            String sql = "UPDATE first_table SET item_name = ?, Value = ? WHERE Id = ?";
            // 获取连接
            Connection conn = DriverManager.getConnection(url, user, password);
            // 创建PreparedStatement对象
            PreparedStatement pstmt = conn.prepareStatement(sql);
            // 设置参数值
            pstmt.setString(1, newItemName);
            pstmt.setString(2, newValue);
            pstmt.setInt(3, idToUpdate);

            // 执行更新操作
            int rowsAffected = pstmt.executeUpdate();

            if (rowsAffected > 0) {
                System.out.println("Record with ID " + idToUpdate + " has been updated.");
            } else {
                System.out.println("No record found with ID " + idToUpdate + ".");
            }
            // 关闭资源
            pstmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
